<?php
	require_once (dirname(__FILE__)."/../config/Config.inc");
	require_once (dirname(__FILE__)."/../config/api.inc");
	
	$parm = requestArrayParm($_REQUEST);
	
	$result['status'] = '1';
	
	unset($wheres,$pages,$sorts);
	$wheres['cp_status'] = '2';
	$wheres['cp_type'] = '2';
	$wheres['cp_style'] = '1';
	
	
	if(empty($parm['jine']))
	{
		$result['status'] = '0';
		$result['msg'] = '缺少参数jine';
		echoJSON($result);
		exit;
	}
	
	if(!empty($parm['qixian']))
	{
		if($parm['qixian'] == '1')
		{
			unset($query,$between);
			$query['cp_status'] = '2';
			$query['cp_tzqx'] = 1;
			$query['cp_tzqxlx'] = 'month';
			
			$resData1 = _SqlTool::getDistinctList('chanpin', $query,'id', $between);
			
			unset($query,$between);
			$query['cp_status'] = '2';
			$query['cp_tzqx'] = array('<=',30);
			$query['cp_tzqxlx'] = 'day';
			
			$resData2 = _SqlTool::getDistinctList('chanpin', $query,'id', $between);
		}
		
		if($parm['qixian'] == '2')
		{
			unset($query,$between);
			$query['cp_status'] = '2';
			$query['cp_tzqx'] = array('<=',3);
			$query['cp_tzqxlx'] = 'month';
			$resData1 = _SqlTool::getDistinctList('chanpin', $query,'id', $between);
			
			unset($query,$between);
			$query['cp_status'] = '2';
			$query['cp_tzqx'] = array('<=',90);
			$query['cp_tzqxlx'] = 'day';
			$resData2 = _SqlTool::getDistinctList('chanpin', $query,'id', $between);
		}
		
		if($parm['qixian'] == '3')
		{
			unset($query,$between);
			$query['cp_status'] = '2';
			$query['cp_tzqx'] = array('<=',6);
			$query['cp_tzqxlx'] = 'month';
			$resData1 = _SqlTool::getDistinctList('chanpin', $query,'id', $between);
		
			unset($query,$between);
			$query['cp_status'] = '2';
			$query['cp_tzqx'] = array('<=',180);
			$query['cp_tzqxlx'] = 'day';
			$resData2 = _SqlTool::getDistinctList('chanpin', $query,'id', $between);
		}
		
		if($parm['qixian'] == '4')
		{
			unset($query,$between);
			$query['cp_tzqx'] = array('<=',12);
			$query['cp_tzqxlx'] = 'month';
			$query['cp_status'] = '2';
			$resData1 = _SqlTool::getDistinctList('chanpin', $query,'id', $between);
			
			unset($query,$between);
			$query['cp_status'] = '2';
			$query['cp_tzqx'] = array('<=',365);
			$query['cp_tzqxlx'] = 'day';
			$resData2 = _SqlTool::getDistinctList('chanpin', $query,'id', $between);
		}
		
		unset($cp_ids);
		foreach($resData1['list'] as $key => $value)
		{
			$cp_ids[] = $value['id'];
		}
		
		foreach($resData2['list'] as $key => $value)
		{
			$cp_ids[] = $value['id'];
		}
		
		if(empty($cp_ids))
		{
			$result['shouyi'] = 0;
			$result['cplist'] = array();
			echoJSON($result);
		}
	}
	else
	{
		$result['status'] = '0';
		$result['msg'] = '缺少参数qixian';
		echoJSON($result);
		exit;
	}
	
	if(!empty($parm['ptid']))
	{
		$wheres['pt_id'] = array('not in',$parm['ptid']);
	}
	
	$wheres['id'] = array('in',join(',',$cp_ids));
	
	$sorts['cp_yqnh'] = 'desc';
	
	$resData =_SqlTool::getList('chanpin', $wheres, $pages, $sorts);
	
	unset($sylist,$cplist,$xiane,$jine);
	$jine = floatval($parm['jine']);
	foreach ($resData as $key => $value)
	{
		unset($info,$query);
		$query['id'] = $value['pt_id'];
		$info = _SqlTool::getInfo('pingtai', $query);
		unset($temp);
		$temp['pt_id'] = $value['pt_id'];
		$temp['id'] = $value['id'];
		$temp['logo'] = OSS_URL.$info['pt_logo'];
		$temp['name'] = $value['cp_mc'];
		$temp['style'] = $CP_FS[$value['cp_style']];
		$temp['yqnh'] = $value['cp_yqnh'];
		$temp['zgfl'] = $value['cp_yqnh'];
		$temp['cp_lx'] = $value['cp_lx'];
		$temp['yqsy'] = $temp['lixi'] + $temp['fanli'];
		$temp['qixian'] = $value['cp_tzqx'];
		$temp['qixianlx'] = $value['cp_tzqxlx'];
		$temp['qtje'] = $value['cp_qtje'];
		$xiane = $temp['xiane'] = $value['cp_xiane'];
		
		if(intval($xiane) == 0 || $xiane >= $jine)
		{
			//计算出利息及返利之和（投资额10000元*年化收益率10+1%*投资天数/365）
			if($value['cp_tzqxlx'] == 'day')
			{
				$tzqx = $value['cp_tzqx'];
			}
			
			if($value['cp_tzqxlx'] == 'month')
			{
				if($value['cp_tzqx'] == '12')
				{
					$tzqx = 365;
				}
				else
				{
					$tzqx = $value['cp_tzqx'] * 30;
				}
			}
			$temp['tzqx'] = $tzqx;
			$temp['jine'] = $jine;
			$temp['cp_lx'] = $value['cp_lx'];
			$temp['cp_fl'] = $value['cp_fl'];
			$temp['lixi'] =	round($jine * ($value['cp_lx'] / 100 ) * $tzqx / 365);
			$temp['fanli'] = round($jine * ($value['cp_fl'] / 100 ) * $tzqx / 365);
			$temp['shouyi'] = $temp['lixi'] + $temp['fanli'];
			$sylist[] = $temp['shouyi'];
			$cplist[] = $temp;
			break;
		}
		else
		{
			$jine = floatval($parm['jine']) - $value['cp_xiane'];
			
			//计算出利息及返利之和（投资额10000元*年化收益率10+1%*投资天数/365）
			if($value['cp_tzqxlx'] == 'day')
			{
				$tzqx = $value['cp_tzqx'];
			}
			
			if($value['cp_tzqxlx'] == 'month')
			{
				if($value['cp_tzqx'] == '12')
				{
					$tzqx = 365;
				}
				else
				{
					$tzqx = $value['cp_tzqx'] * 30;
				}
			}
			$temp['tzqx'] = $tzqx;
			$temp['jine'] = $value['cp_xiane'];
			$temp['cp_lx'] = $value['cp_lx'];
			$temp['cp_fl'] = $value['cp_fl'];
			$temp['lixi'] =	round($value['cp_xiane'] * ($value['cp_lx'] / 100 ) * $tzqx / 365);
			$temp['fanli'] = round($value['cp_xiane'] * ($value['cp_fl'] / 100 ) * $tzqx / 365);
			$temp['shouyi'] = $temp['lixi'] + $temp['fanli'];
			$sylist[] =$temp['shouyi'];
			$cplist[] = $temp;
		}
	}

	$result['cplist'] = $cplist;
	$result['shouyi'] = array_sum($sylist);
	
	echoJSON($result);